import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template("给我讲一个关于 {topic} 的笑话。")

model = ChatOpenAI(
    model="deepseek-chat",
    openai_api_key=os.getenv("DEEPSEEK_API_KEY"),
    openai_api_base=os.getenv("DEEPSEEK_API_BASE")
)
output_parser = StrOutputParser() # 将聊天消息解析为字符串

# 构建链
chain = prompt | model | output_parser

# 调用链
result = chain.invoke({"topic": "程序员"})
print(result)